package com.rvalerio.fgchecker.detectors;

import ohos.app.Context;
import ohos.bundleactive.BundleActiveInfosMgr;
import ohos.bundleactive.BundleActiveStates;

public class LollipopDetector implements Detector {

    @Override
    public String getForegroundApp(Context context) {
        String foregroundApp = null;
        long time = System.currentTimeMillis();
        BundleActiveInfosMgr bundleActiveInfosMgr = BundleActiveInfosMgr.newInstance(context);
        BundleActiveStates bundleActiveStates = bundleActiveInfosMgr.queryBundleActiveStates(time - 1000 * 3600, time);
        BundleActiveStates.State state = new BundleActiveStates.State();
        while (bundleActiveStates.hasNextState()) {
            bundleActiveStates.queryNextState(state);
            if (state.queryStateType() == state.STATE_TYPE_ABILITY_RESUMED) {
                foregroundApp = state.queryBundleName();
            }
        }
        return foregroundApp;
    }
}
